﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class UserInfos : System.Web.UI.PageEx
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string strResult = string.Empty;
            if (!DalEx.DbUserDalEx.CheckUserLogin(UserType, UserId, ref strResult))
            {
                MessageBox(strResult, "UserLogin.aspx");
                return;
            }
            string strLink = "<li><a href=\"{0}\" class=\"{2}\">{1}</a></li>";
            StringBuilder sbMenuLinks = new StringBuilder();
            if (UserType == 1 && UserId > 0)
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MemberCenter.aspx", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "UserInfos.aspx", "我的资料", "on" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "ModifyPwd.aspx", "修改密码", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyBaoMing.aspx", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyYiGongBaoMing.aspx", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "MyHomeEventBaoMing.aspx", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "Logout.aspx", "退出系统", "" }));
            }
            else
            {
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "会员中心", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的资料", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "修改密码", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "我的报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "义工报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "家园活动报名表", "" }));
                sbMenuLinks.Append(string.Format(strLink, new object[] { "#", "退出系统", "" }));
            }
            litMenuLinks.Text = sbMenuLinks.ToString();

            DataBinds();
        }
    }
    private void DataBinds()
    {
        if (UserType == 1 && UserId > 0)
        {
            Model.DbUsers dbUser = DalEx.DbUserDalEx.Select(UserId);
            if (dbUser != null)
            {
                hfUserId.Value = dbUser.UserID.ToString();
                txtUserName.Text = dbUser.UserName;
                rblSex.SelectedValue = dbUser.Sex.ToString();
                txtFirstName.Text = dbUser.FirstName;
                txtLastName.Text = dbUser.LastName;
                txtTel.Text = dbUser.Tel;
                txtAddress.Text = dbUser.Address;
                txtEmail.Text = dbUser.Email;
            }
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        int iUserId = 0;
        int.TryParse(hfUserId.Value, out iUserId);
        string strResult = string.Empty;

        Model.DbUsers dbUser = DalEx.DbUserDalEx.Select(UserId);
        if (dbUser != null)
        {
            short sSex = 0;
            string strFirstName = SqlHelper.String(txtFirstName.Text);
            string strLastName = SqlHelper.String(txtLastName.Text);
            short.TryParse(rblSex.SelectedValue, out sSex);
            string strTel = SqlHelper.String(txtTel.Text);
            string strAddress = SqlHelper.String(txtAddress.Text);
            string strEmail = SqlHelper.String(txtEmail.Text);

            dbUser.FirstName = strFirstName;
            dbUser.LastName = strLastName;
            dbUser.Tel = strTel;
            dbUser.Address = strAddress;
            dbUser.Email = strEmail;
            dbUser.Sex = sSex;
            if (DalEx.DbUserDalEx.Insert(dbUser, ref strResult, ref iUserId) > 0)
            {
                MessageBox1(strResult, "MemberCenter.aspx");
            }
            else
            {
                MessageBox(strResult);
            }
        }
        else
        {
            MessageBox("抱歉，您登录已超时或尚未登录，不能修改资料！");
        }
    }
}
